package util;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;

/**
 * @author zhongwen
 * @description 运行Run线程的类
 * @date 2021-12-25 20:06
 */
public class ProcessExecutor {
    private Process p;
    private JTextArea jTextArea = new JTextArea();

    public ProcessExecutor(Process p) {
        this.p = p;
    }


    public int execute(JTextArea jTextArea) {
        this.jTextArea = jTextArea;
        int rs = 0;
        ProcessOutputThread errorThread = new ProcessOutputThread(p.getErrorStream(), jTextArea);
        ProcessOutputThread outputThread = new ProcessOutputThread(p.getInputStream(), jTextArea);
        ProcessInputThread inputThread = new ProcessInputThread(p.getOutputStream(), jTextArea);
        try {
            inputThread.start();
            inputThread.join();
            outputThread.start();
            outputThread.join();
            errorThread.start();
            errorThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return rs;
    }
}

/**
 * 输出流处理线程
 */
class ProcessOutputThread extends Thread {
    private InputStream in;
    private JTextArea jTextArea;

    public ProcessOutputThread(InputStream in, JTextArea jTextArea) {
        this.in = in;
        this.jTextArea = jTextArea;
    }


    @Override
    public void run() {
        super.run();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "gbk"));
            String s;
            while ((s = reader.readLine()) != null) {
                jTextArea.append(s + "\n");
                jTextArea.setCaretPosition(jTextArea.getText().length());
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/**
 * 输入流处理线程
 */
class ProcessInputThread extends Thread {
    private OutputStream output;
    private BufferedWriter writer;
    private JTextArea jTextArea;
    private StringBuilder sb = new StringBuilder();

    public ProcessInputThread(OutputStream output, JTextArea jTextArea) {
        this.output = output;
        this.jTextArea = jTextArea;

        jTextArea.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                System.out.println(e.getKeyChar() + "被按下了");
                if (e.getKeyChar() == KeyEvent.VK_BACK_SPACE) {
                    sb.deleteCharAt(sb.length() - 1);
                } else if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                    try {
                        sb.append(e.getKeyChar());
                        System.out.println(sb);
                        synchronized (jTextArea) {
                            writer.write(sb.toString());
                            writer.flush();
                        }
                        sb.delete(0, sb.length());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }

                } else {
                    sb.append(e.getKeyChar());
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
            }
        });
    }

    @Override
    public void run() {
        super.run();
        try {
            writer = new BufferedWriter(new OutputStreamWriter(output, "gbk"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

}
